<!-- index.vue -->
<template>
    <div class="container">
        <div class="store-list">
            <div
                v-for="store in stores"
                :key="store.id"
                class="store-item"
                :class="{ active: currentStore.id === store.id }"
                @click="switchStore(store)"
            >
                <div
                    class="store-background"
                    :style="{ backgroundImage: `url(${store.backgroundImage})` }"
                ></div>
                <div class="store-info">
                    <h2>{{ store.name }}</h2>
                    <p>台桌数量: {{ store.tables.length }}</p>
                    <p>当日实收金额合计: {{ getDailyIncome(store) }}</p>
                </div>
            </div>
        </div>
        <div class="table-list">
            <div
                v-for="table in currentStore.tables"
                :key="table.id"
                class="table-item"
                @click="goToTableDetail(table.id)"
            >
                <p>台桌编号: {{ table.id }}</p>
                <p>当日开桌次数: {{ table.openCount }}</p>
                <p>当日合计实收金额: {{ table.totalActualAmount }}</p>
                <p>预收金额: {{ table.totalPreAmount }}</p>
                <p>状态: {{ table.status }}</p>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import { onShow } from '@dcloudio/uni-app';

const vuexStore = useStore();
const stores = computed(() => vuexStore.state.stores);
const currentStore = ref(stores.value[0]);

const switchStore = (store) => {
    currentStore.value = store;
    vuexStore.commit('setCurrentStore', store);
};

const goToTableDetail = (tableId) => {
    console.log('Going to table detail with ID:', tableId);
    uni.navigateTo({
        url: `/pages/tableDetail/tableDetail?tableId=${tableId}`,
        success: () => {
            console.log('跳转成功');
        },
        fail: (err) => {
            console.error('跳转失败', err);
        }
    });
};

const getDailyIncome = (store) => {
    return store.tables.reduce((total, table) => total + table.totalActualAmount, 0);
};

onShow(() => {
    // 重新获取最新的数据
    const newStores = vuexStore.state.stores;
    stores.value = newStores;
    currentStore.value = stores.value[0];
});
</script>


<style scoped>
.container {
    padding: 20px;
}

.store-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
}

.store-item {
    position: relative;
    width: calc(50% - 10px);
    height: 200px;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.3s;
}

.store-item.active {
    transform: scale(1.05);
}

.store-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    filter: brightness(0.7);
}

.store-info {
    position: relative;
    z-index: 1;
    color: white;
    padding: 20px;
}

.store-info h2 {
    margin: 0;
}

.store-info p {
    margin-top: 5px;
}

.table-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

.table-item {
    border: 1px solid #ccc;
    padding: 10px;
    border-radius: 8px;
}
</style>